package first;
import java.io.*;
import java.util.*;
import javax.servlet.*;
import javax.servlet.http.*;
public class DisplayContacts extends HttpServlet{
	private Vector<Contact> contacts=new Vector<Contact>();
	private String dspHomePhone;
	private String dspWorkPhone;
	private String dspEmail;
	private String dspMsn;
	private void loadContacts() {
		Contact c1=new Contact("李","219389","807199354","ajfkdsjlj@gkdsj.com","jsdj@fjdsj.net");
		Contact c2=new Contact("钱","219389","807199354","ajfkdsjlj@gkdsj.com","jsdj@fjdsj.net");
		Contact c3=new Contact("孙","219389","807199354","ajfkdsjlj@gkdsj.com","jsdj@fjdsj.net");
		Contact c4=new Contact("赵","219389","807199354","ajfkdsjlj@gkdsj.com","jsdj@fjdsj.net");
		Contact c5=new Contact("周","219389","807199354","ajfkdsjlj@gkdsj.com","jsdj@fjdsj.net");
		contacts.add(c1);
		contacts.add(c2);
		contacts.add(c3);
		contacts.add(c4);
		contacts.add(c5);
	}
	@Override
	public void init()throws ServletException{
		ServletConfig sc=getServletConfig();
		dspHomePhone=sc.getInitParameter("dspHomePhone");
		if(dspHomePhone==null) {
			dspHomePhone="0";
		}
		dspWorkPhone=sc.getInitParameter("dspWorkPhone");
		if(dspWorkPhone==null) {
			dspWorkPhone="0";
		}
		dspEmail=sc.getInitParameter("dspEmail");
		if(dspEmail==null) {
			dspEmail="0";
		}
		dspMsn=sc.getInitParameter("dspMsn");
		if(dspMsn==null) {
			dspMsn="0";
		}
	}
	@Override
	protected void doPost(HttpServletRequest request,HttpServletResponse response)throws ServletException,IOException{
		doGet(request,response);
	}
	@Override
	protected void doGet(HttpServletRequest request,HttpServletResponse response)throws ServletException,IOException{
		loadContacts();
		response.setContentType("text/html;charset=gbk");
		PrintWriter out=response.getWriter();
		out.println("<html>");
		out.println("<head><title>Contacts</title></head>");
		out.println("<body>");
		for(int i=0;i<contacts.size();i++) {
			Contact ct=contacts.get(i);
			out.print(ct.name);
			out.print("&nbsp;&nbsp;");
			if(dspHomePhone.equals("1")) {
				out.print(ct.homePhone);
				out.print("&nbsp;&nbsp;");
			}
			if(dspWorkPhone.equals("1")) {
				out.print(ct.workPhone);
				out.print("&nbsp;&nbsp;");
			}
			if(dspEmail.equals("1")) {
				out.print(ct.email);
				out.print("&nbsp;&nbsp;");
			}
			if(dspMsn.equals("1")) {
				out.print(ct.msn);
				out.print("&nbsp;&nbsp;");
			}
			out.println("<br/>");
		}
		out.println("</body>");
		out.println("</html>");
	}
}
